--
-- Author: shunguo.chen
-- Date: 2018-04-12 18:02:54
--
-- PvpLoadingView pvp loading界面，有双方对战信息
--

local PvpLoadingRender = import(".renders.PvpLoadingRender")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ViewBase)

ClassRef.VIEW_ROOT_RES = Res.PvpLoadingScene

function ClassRef:init()
	self._ui.bg:setContentSize(display.size)
	self._ui.tlt:setString(L("loadingtips_919"))
end

-- info = PvpLoadingInfo
function ClassRef:refresh(info)
	local teams = info.teams

	local panel = self._ui.Panel
	local renders = {}

	for i,team in ipairs(teams) do
		local pre = i==1 and "b_" or "r_"
		for j,member in ipairs(team) do
			local node = panel:getChildByName(pre .. j)
			local render = PvpLoadingRender.create()
			render:setPosition(node:getPosition())
			render:setIsMy(i==1)
			render:setData(member)
			node:removeFromParent()
			panel:addChild(render)

			local uid = member.baseInfo.uid
			renders[uid] = render
		end
	end

	local autoRenders = {}
	local myRenders = {}
	for _,render in pairs(renders) do
		if render:isUseMy() then
			myRenders[#myRenders + 1] = render
		else
			autoRenders[#autoRenders + 1] = render
		end
	end

	self._renders = renders
	self._myRenders = myRenders
	self._autoRenders = autoRenders

	if #autoRenders > 0 then
		self:scheduleUpdate(handler(self, self.tick),1/10)
	end

	if info.targetID == Enums.TeamTarget.duel then
		self:showNoticeMsg(false)
	else
		self:showNoticeMsg(true)
	end

	if info.targetID == Enums.TeamTarget.kof or info.targetID == Enums.TeamTarget.chess then
		-- kof 和 星空谜棋是单人玩法，等待玩法超时失败消息之后直接关闭loading界面并调用C2S_BackToLastMap

	else
		-- 默认20秒超时，超时玩法会继续进行，超时关闭loading界面
		self:performWithDelay(function()
			print("PvpLoadingView timeout")
			executeFunc(self._onOperate, "timeout")
		end, 20)
	end
end

-- 默认3秒加载时间
function ClassRef:tick(dt)
	for _,render in ipairs(self._autoRenders) do
		render:addProgress(3)
	end
end

-- 更新我自己／机器人的进度
function ClassRef:updateMyPg(pg)
	for _,render in pairs(self._myRenders) do
		render:setProgress(pg)
	end
end

-- datas = {{uid=, progress=}, ...}
function ClassRef:updatePg(datas)
	for i,data in ipairs(datas) do
		local render = self._renders[data.uid]
		render:setProgress(math.floor(data.progress))
	end
end

function ClassRef:showNoticeMsg(visible)
	self._ui.tlt:setVisible(visible)
end

return ClassRef
